﻿Imports TBCC.UtilityMobile




Public Class StartUpTypeService

    Private _pi As ProjectItem
    Private _netid As Integer
    Private pid_str As String

    Public Sub New(ByVal pi As ProjectItem)
        _pi = pi
        _netid = 1
        pid_str = Trim(pi.ProjectID).Replace("-", "_")
    End Sub

    Public Function GetStartUpGroupList(ByVal NeedDetail As Boolean) As IList(Of StartUpTypeItem)

        Return GetStartUpGroupList(Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, NeedDetail)

    End Function

    Public Function GetStartUpGroupList(ByVal BeginTime As DateTime, ByVal EndTime As DateTime, ByVal NeedDetail As Boolean) As IList(Of StartUpTypeItem)

        Return GetStartUpGroupList(BeginTime, EndTime, Nothing, Nothing, Nothing, Nothing, Nothing, NeedDetail)

    End Function

    Public Function GetStartUpGroupList(ByVal BeginTime As DateTime, ByVal EndTime As DateTime, ByVal BeginAddress As String, ByVal EndAddress As String, ByVal Shipper As String, ByVal Carrier As String, ByVal Receiver As String, ByVal NeedDetail As Boolean) As IList(Of StartUpTypeItem)
        Dim il As IList(Of StartUpTypeItem)
        il = New List(Of StartUpTypeItem)

        Dim dr As MySql.Data.MySqlClient.MySqlDataReader

        Dim strSql As String
        strSql = String.Format("SELECT * FROM TbccHDStartUp_{0} WHERE 1=1 ", pid_str)

        strSql &= "AND "
        If BeginTime <> Nothing Then
            strSql &= "BeginTime>="
        End If
        strSql &= "? "

        strSql &= "AND "
        If EndTime <> Nothing Then
            strSql &= "EndTime<="
        End If
        strSql &= "? "

        strSql &= "AND "
        If BeginAddress <> Nothing Then
            strSql &= "BeginAddress LIKE "
        End If
        strSql &= "? "

        strSql &= "AND "
        If EndAddress <> Nothing Then
            strSql &= "EndAddress LIKE "
        End If
        strSql &= "? "

        strSql &= "AND "
        If Shipper <> Nothing Then
            strSql &= "Shipper LIKE "
        End If
        strSql &= "? "

        strSql &= "AND "
        If Carrier <> Nothing Then
            strSql &= "Carrier LIKE "
        End If
        strSql &= "? "

        strSql &= "AND "
        If Receiver <> Nothing Then
            strSql &= "Receiver LIKE "
        End If
        strSql &= "? "

        strSql &= "ORDER BY BeginTime"

        Dim p(6) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p(0).Value = IIf(BeginTime <> Nothing, BeginTime, 1)
        p(1).Value = IIf(EndTime <> Nothing, EndTime, 1)
        p(2).Value = IIf(BeginAddress <> Nothing, String.Format("%{0}%", BeginAddress), 1)
        p(3).Value = IIf(EndAddress <> Nothing, String.Format("%{0}%", EndAddress), 1)
        p(4).Value = IIf(Shipper <> Nothing, String.Format("%{0}%", Shipper), 1)
        p(5).Value = IIf(Carrier <> Nothing, String.Format("%{0}%", Carrier), 1)
        p(6).Value = IIf(Receiver <> Nothing, String.Format("%{0}%", Receiver), 1)

        dr = TBCC.UtilityMobile.SqlHelper.ExecuteDataReader(strSql, p)

        If dr IsNot Nothing Then
            Dim seqNo As Integer = 1
            Do While dr.Read()
                Dim sti As New StartUpTypeItem()
                sti.SeqNo = seqNo
                sti.ID = dr.Item("ID")
                sti.NetID = dr.Item("NetID")
                sti.BeginTime = dr.Item("BeginTime")
                sti.EndTime = dr.Item("EndTime")
                sti.PageIndex = dr.Item("PageIndex")
                sti.PageCount = dr.Item("PageCount")
                sti.RecordInterval = dr.Item("RecordInterval")
                sti.BeginAddress = dr.Item("BeginAddress").ToString()
                sti.EndAddress = dr.Item("EndAddress").ToString()
                sti.Shipper = dr.Item("Shipper").ToString()
                sti.Carrier = dr.Item("Carrier").ToString()
                sti.Receiver = dr.Item("Receiver").ToString()
                sti.LastPageIndex = dr.Item("LastPageIndex")
                sti.LastPageType = dr.Item("LastPageType")
                sti.LastRecordTime = dr.Item("LastRecordTime")
                sti.UploadStatus = dr.Item("UploadStatus")

                If NeedDetail = True Then
                    Dim sdts As New StartUpDetailTypeService(_pi, sti)
                    Dim sdgl As IList(Of StartUpDetailTypeItem)
                    sdgl = sdts.GetStartUpDetailGroupList(sti.ID)

                    If sdgl IsNot Nothing Then
                        For Each sdti As StartUpDetailTypeItem In sdgl
                            sti.DetailItemList.Add(sdti)
                        Next
                    End If
                End If

                seqNo += 1
                il.Add(sti)
            Loop
        End If

        Return il
    End Function

    Public Function FillStartUpTypeItem(Of T1 As {StartUpTypeItem(Of T2), New}, T2 As {StartUpDetailTypeItem, New})(ByVal sti As T1) As Boolean

        Dim sdts As New StartUpDetailTypeService(_pi, sti)
        Dim sdgl As IList(Of T2)

        sdgl = sdts.GetStartUpDetailGroupList(Of T2)(sti.ID)

        If sdgl IsNot Nothing Then
            For Each sdti As T2 In sdgl
                sti.DetailItemList.Add(sdti)
            Next
        End If

        Return True

    End Function

    Public Function InsertStartUpTypeItem(ByVal sti As StartUpTypeBase) As Boolean

        Dim strSql As String
        strSql = String.Format("INSERT INTO TbccHDStartUp_{0}(NetID,BeginTime,EndTime,PageIndex,PageCount,RecordInterval,BeginAddress,EndAddress,Shipper,Carrier,Receiver,UploadStatus,LastRecordTime,LastPageIndex,LastPageType) " _
                            & "VALUES(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14)", pid_str)

        Dim p(14) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = sti.NetID
        p(1).Value = sti.BeginTime
        p(2).Value = sti.EndTime
        p(3).Value = sti.PageIndex
        p(4).Value = sti.PageCount
        p(5).Value = sti.RecordInterval
        p(6).Value = sti.BeginAddress
        p(7).Value = sti.EndAddress
        p(8).Value = sti.Shipper
        p(9).Value = sti.Carrier
        p(10).Value = sti.Receiver
        p(11).Value = sti.UploadStatus
        p(12).Value = sti.LastRecordTime
        p(13).Value = sti.LastPageIndex
        p(14).Value = sti.LastPageType

        If SqlHelper.ExecuteNonQuery(strSql, p) > 0 Then
            '  strSql = String.Format("SELECT seq FROM sqlite_sequence WHERE name='TbccHDStartUp_{0}'", pid_str)
            sti.ID = SqlHelper.ExecuteScalar(strSql, Nothing)
            Return True
        Else
            Return False
        End If

    End Function

    Public Function UpdateStartUpTypeItem(ByVal sti As StartUpTypeBase) As Boolean

        Dim strSql As String
        strSql = String.Format("UPDATE TbccHDStartUp_{0} SET " _
                            & "NetID=?p0,BeginTime=?p1,EndTime=?p2,PageIndex=?p3,PageCount=?p4,RecordInterval=?p5," _
                            & "BeginAddress=?p6,EndAddress=?p7,Shipper=?p8,Carrier=?p9,Receiver=?p10," _
                            & "UploadStatus=?p11,LastRecordTime=?p12,LastPageIndex=?p13,LastPageType=?p14 " _
                            & "WHERE ID=?p15 ", pid_str)

        Dim p(15) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = sti.NetID
        p(1).Value = sti.BeginTime
        p(2).Value = sti.EndTime
        p(3).Value = sti.PageIndex
        p(4).Value = sti.PageCount
        p(5).Value = sti.RecordInterval
        p(6).Value = sti.BeginAddress
        p(7).Value = sti.EndAddress
        p(8).Value = sti.Shipper
        p(9).Value = sti.Carrier
        p(10).Value = sti.Receiver
        p(11).Value = sti.UploadStatus
        p(12).Value = sti.LastRecordTime
        p(13).Value = sti.LastPageIndex
        p(14).Value = sti.LastPageType
        p(15).Value = sti.ID

        Return SqlHelper.ExecuteNonQuery(strSql, p)

    End Function

    Public Function UpdateStartUpTypeItem(ByVal ID As Integer, ByVal UploadStatus As StartUploadStatus, ByVal LastRecordTime As DateTime, ByVal LastPageIndex As Integer, ByVal LastPageType As StartUploadPageType) As Boolean

        Dim strSql As String
        strSql = String.Format("UPDATE TbccHDStartUp_{0} SET " _
                            & "UploadStatus=?p0,LastRecordTime=?p1,LastPageIndex=?p2,LastPageType=?p3 " _
                            & "WHERE ID=?p4 ", pid_str)

        Dim p(4) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = UploadStatus
        p(1).Value = LastRecordTime
        p(2).Value = LastPageIndex
        p(3).Value = LastPageType
        p(4).Value = ID

        Return SqlHelper.ExecuteNonQuery(strSql, p)

    End Function

    Public Function DeleteStartUpTypeItem(ByVal sti As StartUpTypeItem) As Boolean
        Dim tic As New TBCC.UtilityMobile.TranstrationItem()
        Dim strSql As String
        Dim index As Integer

        strSql = String.Format("DELETE FROM TbccHDStartUp_{0} Where ID={1} -- " & index, pid_str, sti.ID)
        tic.Add(strSql, Nothing)
        index += 1

        strSql = String.Format("DELETE FROM TbccHDStartUpDetail_{0} Where ParentID={1} -- " & index, pid_str, sti.ID)
        tic.Add(strSql, Nothing)
        index += 1

        'RecordTime>='{2}' AND RecordTime<='{3}'
        strSql = String.Format("DELETE FROM TbccHDData_{0}_{1} Where ParentID='{2}' -- " & index, _
                                pid_str, _netid, sti.ID)
        tic.Add(strSql, Nothing)
        index += 1

        Return SqlHelper.ExecuteTranstration(tic)
    End Function

End Class
